package 每日一题;

import java.util.ArrayDeque;
import java.util.Deque;

public class 合法括号序列判断 {
    public boolean chkParenthesis(String A, int n) {
        // write code here
        if(n%2!=0) {
            return false;
        }

        Deque<Character> stack=new ArrayDeque<>();
        for (int i = 0; i <n; i++) {
            if (A.charAt(i)=='(') {
                stack.push(A.charAt(i));
            }else if (A.charAt(i)==')' && !stack.isEmpty()){
                stack.pop();
            }else {
                return false;
            }
        }

        return stack.isEmpty();
    }
}
